//
//  HWTeaSelectAlertView.m
//  HuaWan
//
//  Created by HuaWan on 16/10/17.
//  Copyright © HuaWan. All rights reserved.
//

#import "HWTeaSelectAlertView.h"

@implementation HWTeaSelectAlertView

+ (id)instanceView {
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Do any additional setup after loading the view.
}

//显示
- (void)show {
    NSArray *ary = [kDataManager getTypeArray];
    NSInteger x = ary.count / 5;
    NSInteger y = ary.count % 5;
    if (y > 0) {
        x += 1;
    }
    
    NSInteger lineHeight = 100;
    
    _viewHeight.constant = 295 - 200 + x*lineHeight;
    
    
    //移除之前的子控件
    [_myView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    float width = (kScreenWidth - 15*2) / 5;

    __block NSInteger lincount = 0;
    
    [ary enumerateObjectsUsingBlock:^(HWTypeModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger i = idx % 5;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width*i,lineHeight*lincount,width,lineHeight)];
        view.backgroundColor = [UIColor clearColor];

        if (model.ico.length > 0) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = view.bounds;
            button.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 15, 0);
            
            if (!model.isCustom && [model.name isEqualToString:@"自定义"]) {
                button.tag = 9;
            } else {
                button.tag = 10 + idx;
            }
            [button setImage:[UIImage imageNamed:model.ico] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:button];
        }
     
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, view.frame.size.height - 30, view.frame.size.width, 20)];
        label.text = model.name;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:14];
        label.textColor = UIColorFromHex(0x000000);
        
        [view addSubview:label];
        
        [_myView addSubview:view];
        
        if (idx % 5 == 4) {
            lincount ++;
        }
    }];

    self.hidden = NO;
        
    [UIView animateWithDuration:0.5 animations:^{
        self.maskView.alpha = 0.5;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.5 animations:^{
       self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
       self.hidden = YES;
    }];
}

#pragma mark  FKDJoinAtlasCellDelegat 取消
    
- (void)buttonAction:(UIButton *)sender {
    if (_buttonAction) {
        _buttonAction(self,sender);
    }
    [self dismiss];
}

- (IBAction)tapButtonAction:(UITapGestureRecognizer *)sender {
    if (_buttonAction) {
        _buttonAction(self,nil);
    }
    [self dismiss];
}

@end
